//
//  LeftViewController.m
//  瞬间
//
//  Created by dlios on 15-6-30.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "LeftViewController.h"
#import "RadioViewController.h"
#import "AFNetworking.h"
#import "UIImageView+WebCache.h"
#import "adaptModel.h"
#import "adaptViewCell.h"
#import "MyModel.h"
#import "MJRefresh.h"

@interface LeftViewController ()<UITableViewDataSource, UITableViewDelegate>


@property (nonatomic, retain) UIButton *button;
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) UIView *headerView;
@property (nonatomic, retain) UIScrollView *scrollView;
@property (nonatomic, retain) UIImageView *imageView;
@property (nonatomic, retain) NSMutableArray *imageArr;
@property (nonatomic, retain) NSMutableArray *cellArr;
@property (nonatomic, assign) NSInteger start;

@end

@implementation LeftViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];

    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
    self.button.frame = CGRectMake(10, 20, 40, 40);
    [self.button setImage:[UIImage imageNamed:@"1"] forState:UIControlStateNormal];
    [self.view addSubview:self.button];
    [self.button addTarget:self action:@selector(openDrawer:) forControlEvents:UIControlEventTouchUpInside];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [_tableView release];
    
    self.headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 200)];
    self.headerView.backgroundColor = [UIColor cyanColor];
    
    self.tableView.tableHeaderView = self.headerView;
    [self.tableView addHeaderWithCallback:^{
    
    [self createData];
    
    }];
    
    [self.tableView addFooterWithCallback:^{
    
    [self createData];
    
    
    }];
    
    [self.tableView addFooterWithTarget:self action:@selector(addFooter)];
    [self createView];
    [self fresh];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.cellArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reuse = @"reuse";
    adaptViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[adaptViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse];
    }
//    cell.textLabel.text = @"1";
    MyModel *my = self.cellArr[indexPath.row];
    cell.smallTitleLabel.text = [NSString stringWithFormat:@"%@ . %@", my.name, my.enname];
    [cell.showImageView sd_setImageWithURL:[NSURL URLWithString:my.coverimg]];
    cell.bigTitleLabel.text = my.title;
    cell.txtLabel.text = my.content;
//    [my release];
    
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    return 320;
}

- (void)addFooter
{
    
    [self.tableView addFooterWithCallback:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            if(self.start < 62){
                self.start+= 10;
                [self fresh];
                [self.tableView footerEndRefreshing];
            }
            else{
                [self.tableView footerEndRefreshing];
            }
        });
    }];
}



// 数据解析
- (void)createData
{
    

//    NSDictionary *bodyDic = [NSDictionary dictionaryWithObjectsAndKeys:@"auth",@"XMY4QH258ahDCMl0GsfqlDJxQ8qqYm2eO8GY6Cm1RjcWP23txPSR5c6v8", @"client",@"1", @"deviceid", @"27E1E230-C58B-4514-BB32-DF37127AC293", @"limit", @"10", @"start",@"0",@"version",@"3.0.3", nil];
    
//    NSString *str = [NSString stringWithFormat:@"%ld", self.start];
    
    
    NSDictionary *bodyDic = @{@"start":@"0",@"client":@"1",@"deviceid":@"9684F7A6-7A7D-412E-A5CF-71B4AAA2161A",@"limit":@"10",@"auth":@"",@"version":@"3.0.0"};
    
    AFHTTPRequestOperationManager *manger = [AFHTTPRequestOperationManager manager];
    
    manger.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json",@"text/javascript",@"application/x-javascript", nil];
    [manger POST:@"http://api2.pianke.me/pub/today" parameters:bodyDic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        self.imageArr = [NSMutableArray array];
        NSMutableArray *tempArr = [NSMutableArray array];
        
        self.cellArr = [NSMutableArray array];
        NSMutableDictionary *dic = responseObject[@"data"];
//        NSLog(@"%@", dic);
        NSArray *arr = dic[@"carousel"];
        for (NSDictionary *tempDic in arr) {
            adaptModel *model = [[adaptModel alloc] init];
            [model setValuesForKeysWithDictionary:tempDic];
            [tempArr addObject:model.img];
//            NSLog(@"%@", model.img);
            }
        NSArray *listArr = dic[@"list"];
        
        for (NSDictionary *tempDic1 in listArr) {
            MyModel *my = [[MyModel alloc] init];
            [my setValuesForKeysWithDictionary:tempDic1];
            [self.cellArr addObject:my];
            NSLog(@"%@", my.name);
            [my release];
        
            [self.tableView reloadData];
            
        }
        [self.imageArr addObject:tempArr[tempArr.count - 1]];
        for (NSInteger i = 0; i < tempArr.count; i++) {
            [self.imageArr addObject:tempArr[i]];
        }

        [self.imageArr addObject:tempArr[0]];
        
        self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width * self.imageArr.count, 300);
        self.scrollView.showsHorizontalScrollIndicator = NO;
        
        for (NSInteger i = 0; i < self.scrollView.subviews.count; i++) {
            UIImageView *imageView = self.scrollView.subviews[i];
            [imageView removeFromSuperview];
        }

        
        for (NSInteger i = 0; i < self.imageArr.count; i++) {
            self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width * i, 0, self.view.frame.size.width, 200)];
            
          [self.scrollView addSubview:self.imageView];

            [self.imageView sd_setImageWithURL:[NSURL URLWithString:self.imageArr[i]]];
            
            [self.imageView release];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    [self.tableView headerEndRefreshing];
}


- (void)fresh
{
    
    
    //    NSDictionary *bodyDic = [NSDictionary dictionaryWithObjectsAndKeys:@"auth",@"XMY4QH258ahDCMl0GsfqlDJxQ8qqYm2eO8GY6Cm1RjcWP23txPSR5c6v8", @"client",@"1", @"deviceid", @"27E1E230-C58B-4514-BB32-DF37127AC293", @"limit", @"10", @"start",@"0",@"version",@"3.0.3", nil];
    
    NSString *str = [NSString stringWithFormat:@"%ld", self.start];
    
    
    NSDictionary *bodyDic = @{@"start":str,@"client":@"1",@"deviceid":@"9684F7A6-7A7D-412E-A5CF-71B4AAA2161A",@"limit":@"10",@"auth":@"",@"version":@"3.0.0"};
    
    AFHTTPRequestOperationManager *manger = [AFHTTPRequestOperationManager manager];
    
    manger.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json",@"text/javascript",@"application/x-javascript", nil];
    [manger POST:@"http://api2.pianke.me/pub/today" parameters:bodyDic success:^(AFHTTPRequestOperation *operation, id responseObject) {
        self.imageArr = [NSMutableArray array];
        NSMutableArray *tempArr = [NSMutableArray array];
        
        self.cellArr = [NSMutableArray array];
        NSMutableDictionary *dic = responseObject[@"data"];
        //        NSLog(@"%@", dic);
        NSArray *arr = dic[@"carousel"];
        for (NSDictionary *tempDic in arr) {
            adaptModel *model = [[adaptModel alloc] init];
            [model setValuesForKeysWithDictionary:tempDic];
            [tempArr addObject:model.img];
            //            NSLog(@"%@", model.img);
        }
        NSArray *listArr = dic[@"list"];
        
        for (NSDictionary *tempDic1 in listArr) {
            MyModel *my = [[MyModel alloc] init];
            [my setValuesForKeysWithDictionary:tempDic1];
            [self.cellArr addObject:my];
            NSLog(@"%@", my.name);
            [my release];
            
            [self.tableView reloadData];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    [self.tableView headerEndRefreshing];
}
- (void)createView
{
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 300)];
    
    self.scrollView.pagingEnabled = YES;
    
    //设置偏移量
    self.scrollView.contentOffset = CGPointMake(self.view.frame.size.width, 0);
    
    [self.headerView addSubview:self.scrollView];
    [_scrollView release];
    
    //加定时器
    [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(changeImage) userInfo:nil repeats:YES];
}

- (void)changeImage
{
    [self.scrollView setContentOffset:CGPointMake(self.scrollView.contentOffset.x + self.view.frame.size.width, 0) animated:YES];
    
    //无限滚
    if (self.scrollView.contentOffset.x == self.view.frame.size.width * (self.imageArr.count - 1)) {
        self.scrollView.contentOffset = CGPointMake(self.view.frame.size.width, 0);
    }
    //所有数据都在block中进行操作
    self.view.backgroundColor = [UIColor cyanColor];
    
    
}



- (void)openDrawer:(UIButton *)button
{

    [self.drawer open];
//    [self openDrawerButton];
//    [self createTableView];
    
}
//// 开启抽屉button
//- (void)openDrawerButton
//{
//    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
//    self.button.frame = CGRectMake(10, 20, 40, 40);
//    [self.button setImage:[UIImage imageNamed:@"1"] forState:UIControlStateNormal];
//    [self.view addSubview:self.button];
//    [self.button addTarget:self action:@selector(openDrawer:) forControlEvents:UIControlEventTouchUpInside];
//}


//- (void)openDrawer:(UIButton *)button
//{
//    [self.drawer open];
//}

- (void)drawerControllerWillOpen:(ICSDrawerController *)drawerController
{
    self.view.userInteractionEnabled = NO;
}

- (void)drawerControllerDidClose:(ICSDrawerController *)drawerController
{
    self.view.userInteractionEnabled = YES;
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
@end
